# 字典储存键/值对
person3 = {
    "Name":"Ford Prefect",
    "Gender":"Male",
    "Occupation":"Researcher",
    "Home Planet":"Betelgeuse Seven"
}
print(person3)  #字典是无序的


# 字典中的中括号
person3["Name"]
print(person3["Name"])

# 回顾：显示找到的元音（vowels3.py）

# 频度计数数据结构的初始化
found = {}
found["a"] = 0
found["e"] = 0
found["i"] = 0
found["o"] = 0
found["u"] = 0

# 频度计数器
# 数据初始化为0后，根据需要来递增特定的值
print(found)
found["e"] = found["e"]+1
print(found)

# 更加简洁的更新频度
found["e"] += 1
print(found)

# 不指定顺序时顺序是无序的
for k in found:
    print(k,"was found",found[k],"time(s).")


print("-----------------------------")
# 指定输出时的字典顺序
for k in sorted(found):
    print(k,"was found",found[k],"time(s).")

# 用items和sorted函数控制输出顺序
for k,v in sorted(found.items()):
    print(k,"was found",v,"time(s).")

# 用in 检查成员关系
fruits = {}
fruits["apples"] = 10
if "bananas"in fruits:
    fruits["bananas"] += 1
else:
    fruits["bananas"] = 1
print(fruits)

# 用not in 替代 in
if "pears" not in fruits:
    fruits["pears"] = 0
fruits["pears"] += 1
print(fruits)

# setdefault方法初始化数据
fruits.setdefault("pears",0) #如果pears不存在的话就会初始化为0，如果已经存在的话则不会起作用
fruits["pears"] +=1
print(fruits)

# 集合

# union表示并集
vowels = set("aeiou")
word = "hello"
u = vowels.union(set(word)) #union合并集合
## vowels-->元音集合,set(word)---->hello作为一个集合
# u 表示两个集合的并集
print(u)

# difference告诉你哪些元素只在一个集合而不在另一个集合
d = vowels.difference(set(word))
print(d)

# intersection表示交集
i = vowels.intersection(set(word))
print(i)

#元组
# 元组一旦创建并填充数据就不可改变
vowels = ["a","e","i","o","u"]
type(vowels)
vowels2 = ("a","e","i","o","u") #小括号表示这个是一个元组
type(vowels2)

vowels[2] = "I"
print(vowels2)

# 注意只有一个对象的元组
t = ("Python")
type(t)
t2 = ("Python",) #末尾的逗号告诉解释器这是一个元组
type(t2)

# 存储一个数据表
people = {}
people["Ford"] = {"Name":"Ford Prefect",
                  "Gener":"Male",
                  "Occupation":"Researcher",
                  "Home Planer":"Betelgeuse Seven"
                  }
print(people)

# 继续在表中增加另外3行数据
people["Arthur"] = {"Name":"Arthur Dent",
                    "Gender":"Male",
                    "Occupation":"Sandwich-Maker",
                    "Home Planet":"Earth"
}
people["Trillian"] = {"Name":"Tricia McMillan",
                      "Gender":"Female",
                      "Occupation":"Mathematician",
                      "Home Planet":"Earth"
}
people["Robot"] = {"Name":"Marvin",
                   "Gender":"Unknown",
                   "Occupation":"Paranoid Android",
                   "Home Planet":"Unknown"

}

print(people)

# 美观的打印复杂的数据结构
# 对我们people嵌套的数据使用pprint模块
import pprint
pprint.pprint(people) # 导入pprint模块，调用pprint函数进行打印

#请求访问Arthur的数据行
print(people["Arthur"])

vowels = set("aeiou")
word = input("Provide a word to search for vowels:")
found = vowels.intersection(set(word))
for vowel in found:
    print(vowel)

